package com.bytech.article.biz.service.articleTag.Impl;

import java.util.List;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytech.common.biz.entity.articleTag.ArticleTag;
import com.bytech.article.biz.mapper.articleTag.ArticleTagMapper;
import com.bytech.article.biz.service.articleTag.ArticleTagService;
import com.bytech.common.biz.entity.articleTag.ArticleTagDto;
import com.bytech.common.biz.entity.articleTag.ArticleTagQuery;
import com.bytech.common.biz.entity.base.BaseResult;
import com.bytech.common.biz.enums.CommonStatusEnum;
import com.bytech.common.biz.service.base.BasicServiceImpl;
import com.bytech.common.utils.StringUtils;

import org.springframework.stereotype.Service;

/**
 * @Author Zhao
 * @Create 2021-08-13
 */
@Service
public class ArticleTagServiceImpl extends BasicServiceImpl<ArticleTagMapper, ArticleTag> implements ArticleTagService {

    @Resource
    private ArticleTagMapper articleTagMapper;

    @Override
    public Page<ArticleTag> pageTagByQuery(ArticleTagQuery<ArticleTag> tagQuery) {
        LambdaQueryWrapper<ArticleTag> queryWrapper = buildTagQuery(tagQuery);
        articleTagMapper.selectPage(tagQuery, queryWrapper);
        return tagQuery;
    }

    @Override
    public BaseResult<ArticleTag> saveTag(ArticleTagDto articleTagDto) {
        BaseResult<ArticleTag> checkResult = checkArticleTag(articleTagDto);
        if(checkResult.isFailure()){
            return checkResult;
        }
        if (articleTagDto.getId() == null) {
            articleTagDto.init();
            save(articleTagDto);
        } else {
            updateById(articleTagDto);
        }
        return BaseResult.<ArticleTag>success("保存成功!").data(articleTagDto).build();
    }

    @Override
    public BaseResult<List<ArticleTagDto>> getAllUsableTagWithArticleCount(){
        return BaseResult.<List<ArticleTagDto>>success().build();
    }

    private BaseResult<ArticleTag> checkArticleTag(ArticleTagDto articleTagDto) {
        if (articleTagDto == null || StringUtils.isBlank(articleTagDto.getTagName())) {
            return BaseResult.<ArticleTag>error("标签名称不能为空").build();
        }
        if (CommonStatusEnum.of(articleTagDto.getTagStatus()).equals(CommonStatusEnum.UNKNOWN)) {
            return BaseResult.<ArticleTag>error("未知的标签状态").build();
        }
        return BaseResult.<ArticleTag>success().build();
    }

    private LambdaQueryWrapper<ArticleTag> buildTagQuery(ArticleTagQuery<ArticleTag> tagQuery) {
        LambdaQueryWrapper<ArticleTag> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isBlank(tagQuery.getTagName())) {
            queryWrapper.like(ArticleTag::getTagName, tagQuery.getTagName());
        }
        if (!StringUtils.isBlank(tagQuery.getTagStatus())) {
            queryWrapper.eq(ArticleTag::getTagStatus, tagQuery.getTagStatus());
        }
        return queryWrapper;
    }
}
